package com.abaaba.project2.controller.wxcontroller;

import com.abaaba.project2.bean.bo.CartAddBo;
import com.abaaba.project2.bean.bo.CartUpdateBo;
import com.abaaba.project2.bean.bo.CheckedBo;
import com.abaaba.project2.bean.bo.wx.DeleteCartBo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.wx.CheckInfoVo;
import com.abaaba.project2.bean.vo.wx.WxCartListVo;
import com.abaaba.project2.service.wx.WxCartService;
import com.abaaba.project2.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/cart")
public class WxCartController {

    static Integer loginUserid;

    @Autowired
    WxCartService wxCartService;

    @RequestMapping("/goodscount")
    public BaseRespVo getGoodsNumInCart()
    {
        loginUserid = UserIdUtil.getUserId();

        Integer totalGoodsNum = wxCartService.getGoodsNumInCart(loginUserid);

        return BaseRespVo.ok(totalGoodsNum);

    }

    @RequestMapping(value = {"/add","/fastadd"})
    public BaseRespVo addGoodsIntoCart(@RequestBody CartAddBo cartAddBo)
    {
        cartAddBo.setUserId(UserIdUtil.getUserId());

        Integer status = wxCartService.addGoodsIntoCart(cartAddBo);

        if (status == 1)
        {
            return BaseRespVo.ok(wxCartService.getGoodsNumInCart(loginUserid));
        }

        return BaseRespVo.fail();
    }

    @RequestMapping("/index")
    public BaseRespVo getCartIndex()
    {
        loginUserid = UserIdUtil.getUserId();

        WxCartListVo wxCartListVo = wxCartService.getCartIndex(loginUserid);

        return BaseRespVo.ok(wxCartListVo);

    }

    @RequestMapping("/update")
    public BaseRespVo updateCart(@RequestBody CartUpdateBo cartUpdateBo)
    {
        cartUpdateBo.setUserId(UserIdUtil.getUserId());

        wxCartService.updateCart(cartUpdateBo);

        return BaseRespVo.ok();
    }

    @RequestMapping("/checked")
    public BaseRespVo checkGoods(@RequestBody CheckedBo checkedBo)
    {
        checkedBo.setUserId(UserIdUtil.getUserId());

        WxCartListVo wxCartListVo = wxCartService.CheckGoods(checkedBo);

        return BaseRespVo.ok(wxCartListVo);
    }

//    checkout?cartId=0&addressId=3&couponId=22&grouponRulesId=0
    @RequestMapping("/checkout")
    public BaseRespVo checkoutInfo(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId)
    {
        CheckInfoVo checkInfoVo = wxCartService.checkoutInfo(cartId,addressId,couponId,grouponRulesId);

        return BaseRespVo.ok(checkInfoVo);
    }

    @RequestMapping("/delete")
    public BaseRespVo deleteCart(@RequestBody DeleteCartBo deleteCartBo)
    {
        deleteCartBo.setUserId(UserIdUtil.getUserId());

        WxCartListVo wxCartListVo = wxCartService.deleteCart(deleteCartBo);

        return BaseRespVo.ok(wxCartListVo);
    }

}
